package com.goodsogood.union.auth.sync;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.kevinsawicki.http.HttpRequest;
import com.goodsogood.service.union.user.thrift.UserInfo;
import com.goodsogood.service.union.user.thrift.UserInfoDetail;
import com.goodsogood.service.union.user.thrift.UserListItem;
import com.goodsogood.union.auth.conf.CommonConfiger;
import com.goodsogood.union.auth.util.IdCardInfo;
import com.goodsogood.union.auth.util.IdCardUtil;
import com.goodsogood.union.auth.util.Md5PwdEncoder;
import com.goodsogood.union.auth.util.UUIDUtils;

/**
 * 获取用户
 * @author liuy
 *
 */
@Component
public class UserInfoGetSynchronizer {
    
    static final String getUserUrl = CommonConfiger.getInstance().getHost() + "/union/syn/user/get";
    final Logger logger = LoggerFactory.getLogger(this.getClass());
	public UserListItem getUserInfoByIdCard(String userIdCard) {
		try {
			String para = " {'account': '" + userIdCard + "'}";
			Map<String, String> params = new HashMap<>();
			params.put("para", para);
			
			logger.info("UserInfoGetSynchronizer.getUserInfo para is {}", params);
			
			String response = HttpRequest.post(
			        getUserUrl,
			        params, true).body();
			
			logger.info("UserInfoGetSynchronizer.getUserInfo response is {}", response);
			
			JSONObject json = JSON.parseObject(response);
			if (StringUtils.isEmpty(json.getString("data"))) {
				return null;
			}
			String data = json.getString("data");
			JSONObject dataJson = JSON.parseObject(data);
			if (dataJson == null || !dataJson.containsKey("infResult")) {
				return null;
			}
			dataJson = JSON.parseObject(dataJson.getString("infResult"));
			if (dataJson.getInteger("status") != 1) {
				return null;
			}
			JSONObject innerData = dataJson.getJSONObject("data");
			if(innerData==null||innerData.isEmpty()){
				return null;
			}
			UserInfo info = new UserInfo();
			UserInfoDetail infoDetail = new UserInfoDetail();
			info.setMobilePhone(innerData.getString("mobilePhone"));
			info.setIdentityCard(innerData.getString("idCard"));
			info.setHeadImgUrl(innerData.getString("userPictureUrl"));
			info.setPasswordSalt(UUIDUtils.uuid32());
			String last6 = info.getIdentityCard().substring(info.getIdentityCard().length()-6, info.getIdentityCard().length());
			info.setPassword(new Md5PwdEncoder().encodePassword(last6, info.getPasswordSalt()));
			IdCardInfo idCardInfo = IdCardUtil.getInfo(info.getIdentityCard());
			if(idCardInfo!=null){
				info.setSex(idCardInfo.getSex());
			}
			info.setRealName(innerData.getString("userName"));
			UserListItem i = new UserListItem();
			i.setInfo(info);
			i.setDetail(infoDetail);
			return i;
		} catch (Exception e) {
		    logger.error("UserInfoGetSynchronizer.getUserInfo userIdCard is {}, happen error ", userIdCard, e);
			return null;
		}

	}
}
